/*
 *
 * Copyright (c) 2021
 * User:Dybala
 * File:UserController.java
 * Date:2021/11/28 20:49:28
 */

package com.controller.UserController;

import com.config.myAnnotation.Log;
import com.config.myAnnotation.PassToken;
import com.pojo.*;
import com.service.MailService;
import com.service.TaskService;
import com.service.UserService;
import com.service.redisService;
import com.utils.HttpContextUtils;
import com.utils.JwtUtils;
import com.utils.VerificationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用户的个人信息控制类，包含修改密码，修改手机号码，修改用户名等。
 */
@RestController
@Api(tags="用户个人信息管理接口")
@RequestMapping("/user/personal")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    redisService redisService;
    @Autowired
    MailService mailService;


    @Log("用户重置密码")
    @ApiOperation("重置密码,参数为code：验证码，newPwd：新密码")
    @PostMapping("/reset")
    @PassToken
    @CrossOrigin
    public Message resetPassword(@RequestBody Map<String, String> map){
//        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
//        String token = request.getHeader("token");
//        String email=map.getOrDefault("email",null);
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email=null;
        if(token!=null)
            email= JwtUtils.getEmail(token);

        String str = map.getOrDefault("email", null);
        if(str!=null)email=str;
//        if(token!=null)
//        email = JwtUtils.getEmail(token);
        String code = map.get("code");
        String newPwd = map.get("newPwd");
        return userService.resetPwd(code,newPwd,email);

    }

    @Log("修改用户名")
    @ApiOperation("修改用户名,参数为newName,新名字")
    @CrossOrigin
    @PostMapping("/resetName")
    public Message resetUserName( @RequestBody Map<String,String> params){
        String newName = params.get("newName");
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        return userService.resetUserName(email, newName);
    }

    @Log("添加手机号")
    @ApiOperation("添加手机号,参数为phone")
    @CrossOrigin
    @PostMapping("/resetPhone")
    public Message resetPhone( @RequestBody Map<String,String> params){
        String phone = params.get("phone");
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        return userService.resetPhone(phone,email);
    }

    @ApiOperation("发送修改密码验证码接口")
    @CrossOrigin
    @PassToken
    @PostMapping("/code")
    public void sendEmail(@RequestBody Map<String,String> params) throws MessagingException {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email=null;
        if(token!=null)
        email= JwtUtils.getEmail(token);

        // 获取验证码
        String str = params.getOrDefault("email", null);
        if(str!=null)email=str;

        String code= VerificationUtils.generateCode();
        // 把校验信息锁到redis里面
        String key=email+"set";
        redisService.set(key,code);
        // 设置10分钟的过期时间
        redisService.expire(key, JwtUtils.EXPIRE_TIME/3);
        // 发送邮件
        mailService.sendSimpleMail(email,"（十分钟内有效,修改密码）您的验证码",code);
//        System.out.println(email);
    }
    @ApiOperation("查看个人信息")
    @CrossOrigin
    @GetMapping("/detail")
    public User queryDetail(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        User user = userService.queryDetails(email);
        return user;
    }

    @ApiOperation("申诉")
    @CrossOrigin
    @Log("用户申诉")
    @PostMapping("/debate")
    @PassToken
    public Message debate(@RequestBody Report report){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        report.setLanucher(email);
        report.setType_id(2);
        Message message = userService.reportToAdmin(report);
        return message;
    }
    @ApiOperation("查看历史评价")
    @CrossOrigin
    @Log("用户查看自己接受任务的评价")
    @GetMapping("/queryComment")
    public List<Comment> queryComment(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        List<Comment> comments = userService.queryComment(email);
        return comments;
    }

    @ApiOperation("初始化页面数据")
    @CrossOrigin
    @Log("初始化页面数据")
    @GetMapping("/initdata")
    public Map<String,Object> initData(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        String email = JwtUtils.getEmail(token);
        Map<String, Object> stringObjectMap = userService.initData(email);
        return stringObjectMap;
    }

    @ApiOperation("读取公告")
    @CrossOrigin
    @Log("读取公告")
    @GetMapping("/readNotice")
    @PassToken
    public List<Notice> readNotice(){
        List<Notice> notices = userService.readNotice();
        return notices;
    }

}
